home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_operator.py < prev    next >
Text File  |  2005-10-18  |  15KB  |  370 lines

  1. import operator
  2. import unittest
  3.  
  4. from test import test_support
  5.  
  6.  
  7. class OperatorTestCase(unittest.TestCase):
  8.     def test_lt(self):
  9.         self.failUnlessRaises(TypeError, operator.lt)
  10.         self.failUnlessRaises(TypeError, operator.lt, 1j, 2j)
  11.         self.failIf(operator.lt(1, 0))
  12.         self.failIf(operator.lt(1, 0.0))
  13.         self.failIf(operator.lt(1, 1))
  14.         self.failIf(operator.lt(1, 1.0))
  15.         self.failUnless(operator.lt(1, 2))
  16.         self.failUnless(operator.lt(1, 2.0))
  17.  
  18.     def test_le(self):
  19.         self.failUnlessRaises(TypeError, operator.le)
  20.         self.failUnlessRaises(TypeError, operator.le, 1j, 2j)
  21.         self.failIf(operator.le(1, 0))
  22.         self.failIf(operator.le(1, 0.0))
  23.         self.failUnless(operator.le(1, 1))
  24.         self.failUnless(operator.le(1, 1.0))
  25.         self.failUnless(operator.le(1, 2))
  26.         self.failUnless(operator.le(1, 2.0))
  27.  
  28.     def test_eq(self):
  29.         class C(object):
  30.             def __eq__(self, other):
  31.                 raise SyntaxError
  32.         self.failUnlessRaises(TypeError, operator.eq)
  33.         self.failUnlessRaises(SyntaxError, operator.eq, C(), C())
  34.         self.failIf(operator.eq(1, 0))
  35.         self.failIf(operator.eq(1, 0.0))
  36.         self.failUnless(operator.eq(1, 1))
  37.         self.failUnless(operator.eq(1, 1.0))
  38.         self.failIf(operator.eq(1, 2))
  39.         self.failIf(operator.eq(1, 2.0))
  40.  
  41.     def test_ne(self):
  42.         class C(object):
  43.             def __ne__(self, other):
  44.                 raise SyntaxError
  45.         self.failUnlessRaises(TypeError, operator.ne)
  46.         self.failUnlessRaises(SyntaxError, operator.ne, C(), C())
  47.         self.failUnless(operator.ne(1, 0))
  48.         self.failUnless(operator.ne(1, 0.0))
  49.         self.failIf(operator.ne(1, 1))
  50.         self.failIf(operator.ne(1, 1.0))
  51.         self.failUnless(operator.ne(1, 2))
  52.         self.failUnless(operator.ne(1, 2.0))
  53.  
  54.     def test_ge(self):
  55.         self.failUnlessRaises(TypeError, operator.ge)
  56.         self.failUnlessRaises(TypeError, operator.ge, 1j, 2j)
  57.         self.failUnless(operator.ge(1, 0))
  58.         self.failUnless(operator.ge(1, 0.0))
  59.         self.failUnless(operator.ge(1, 1))
  60.         self.failUnless(operator.ge(1, 1.0))
  61.         self.failIf(operator.ge(1, 2))
  62.         self.failIf(operator.ge(1, 2.0))
  63.  
  64.     def test_gt(self):
  65.         self.failUnlessRaises(TypeError, operator.gt)
  66.         self.failUnlessRaises(TypeError, operator.gt, 1j, 2j)
  67.         self.failUnless(operator.gt(1, 0))
  68.         self.failUnless(operator.gt(1, 0.0))
  69.         self.failIf(operator.gt(1, 1))
  70.         self.failIf(operator.gt(1, 1.0))
  71.         self.failIf(operator.gt(1, 2))
  72.         self.failIf(operator.gt(1, 2.0))
  73.  
  74.     def test_abs(self):
  75.         self.failUnlessRaises(TypeError, operator.abs)
  76.         self.failUnlessRaises(TypeError, operator.abs, None)
  77.         self.failUnless(operator.abs(-1) == 1)
  78.         self.failUnless(operator.abs(1) == 1)
  79.  
  80.     def test_add(self):
  81.         self.failUnlessRaises(TypeError, operator.add)
  82.         self.failUnlessRaises(TypeError, operator.add, None, None)
  83.         self.failUnless(operator.add(3, 4) == 7)
  84.  
  85.     def test_bitwise_and(self):
  86.         self.failUnlessRaises(TypeError, operator.and_)
  87.         self.failUnlessRaises(TypeError, operator.and_, None, None)
  88.         self.failUnless(operator.and_(0xf, 0xa) == 0xa)
  89.  
  90.     def test_concat(self):
  91.         self.failUnlessRaises(TypeError, operator.concat)
  92.         self.failUnlessRaises(TypeError, operator.concat, None, None)
  93.         self.failUnless(operator.concat('py', 'thon') == 'python')
  94.         self.failUnless(operator.concat([1, 2], [3, 4]) == [1, 2, 3, 4])
  95.  
  96.     def test_countOf(self):
  97.         self.failUnlessRaises(TypeError, operator.countOf)
  98.         self.failUnlessRaises(TypeError, operator.countOf, None, None)
  99.         self.failUnless(operator.countOf([1, 2, 1, 3, 1, 4], 3) == 1)
  100.         self.failUnless(operator.countOf([1, 2, 1, 3, 1, 4], 5) == 0)
  101.  
  102.     def test_delitem(self):
  103.         a = [4, 3, 2, 1]
  104.         self.failUnlessRaises(TypeError, operator.delitem, a)
  105.         self.failUnlessRaises(TypeError, operator.delitem, a, None)
  106.         self.failUnless(operator.delitem(a, 1) is None)
  107.         self.assert_(a == [4, 2, 1])
  108.  
  109.     def test_delslice(self):
  110.         a = range(10)
  111.         self.failUnlessRaises(TypeError, operator.delslice, a)
  112.         self.failUnlessRaises(TypeError, operator.delslice, a, None, None)
  113.         self.failUnless(operator.delslice(a, 2, 8) is None)
  114.         self.assert_(a == [0, 1, 8, 9])
  115.  
  116.     def test_div(self):
  117.         self.failUnlessRaises(TypeError, operator.div, 5)
  118.         self.failUnlessRaises(TypeError, operator.div, None, None)
  119.         self.failUnless(operator.floordiv(5, 2) == 2)
  120.  
  121.     def test_floordiv(self):
  122.         self.failUnlessRaises(TypeError, operator.floordiv, 5)
  123.         self.failUnlessRaises(TypeError, operator.floordiv, None, None)
  124.         self.failUnless(operator.floordiv(5, 2) == 2)
  125.  
  126.     def test_truediv(self):
  127.         self.failUnlessRaises(TypeError, operator.truediv, 5)
  128.         self.failUnlessRaises(TypeError, operator.truediv, None, None)
  129.         self.failUnless(operator.truediv(5, 2) == 2.5)
  130.  
  131.     def test_getitem(self):
  132.         a = range(10)
  133.         self.failUnlessRaises(TypeError, operator.getitem)
  134.         self.failUnlessRaises(TypeError, operator.getitem, a, None)
  135.         self.failUnless(operator.getitem(a, 2) == 2)
  136.  
  137.     def test_getslice(self):
  138.         a = range(10)
  139.         self.failUnlessRaises(TypeError, operator.getslice)
  140.         self.failUnlessRaises(TypeError, operator.getslice, a, None, None)
  141.         self.failUnless(operator.getslice(a, 4, 6) == [4, 5])
  142.  
  143.     def test_indexOf(self):
  144.         self.failUnlessRaises(TypeError, operator.indexOf)
  145.         self.failUnlessRaises(TypeError, operator.indexOf, None, None)
  146.         self.failUnless(operator.indexOf([4, 3, 2, 1], 3) == 1)
  147.         self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
  148.  
  149.     def test_invert(self):
  150.         self.failUnlessRaises(TypeError, operator.invert)
  151.         self.failUnlessRaises(TypeError, operator.invert, None)
  152.         self.failUnless(operator.inv(4) == -5)
  153.  
  154.     def test_isCallable(self):
  155.         self.failUnlessRaises(TypeError, operator.isCallable)
  156.         class C:
  157.             pass
  158.         def check(self, o, v):
  159.             self.assert_(operator.isCallable(o) == callable(o) == v)
  160.         check(self, 4, 0)
  161.         check(self, operator.isCallable, 1)
  162.         check(self, C, 1)
  163.         check(self, C(), 0)
  164.  
  165.     def test_isMappingType(self):
  166.         self.failUnlessRaises(TypeError, operator.isMappingType)
  167.         self.failIf(operator.isMappingType(1))
  168.         self.failIf(operator.isMappingType(operator.isMappingType))
  169.         self.failUnless(operator.isMappingType(operator.__dict__))
  170.         self.failUnless(operator.isMappingType({}))
  171.  
  172.     def test_isNumberType(self):
  173.         self.failUnlessRaises(TypeError, operator.isNumberType)
  174.         self.failUnless(operator.isNumberType(8))
  175.         self.failUnless(operator.isNumberType(8j))
  176.         self.failUnless(operator.isNumberType(8L))
  177.         self.failUnless(operator.isNumberType(8.3))
  178.         self.failIf(operator.isNumberType(dir()))
  179.  
  180.     def test_isSequenceType(self):
  181.         self.failUnlessRaises(TypeError, operator.isSequenceType)
  182.         self.failUnless(operator.isSequenceType(dir()))
  183.         self.failUnless(operator.isSequenceType(()))
  184.         self.failUnless(operator.isSequenceType(xrange(10)))
  185.         self.failUnless(operator.isSequenceType('yeahbuddy'))
  186.         self.failIf(operator.isSequenceType(3))
  187.  
  188.     def test_lshift(self):
  189.         self.failUnlessRaises(TypeError, operator.lshift)
  190.         self.failUnlessRaises(TypeError, operator.lshift, None, 42)
  191.         self.failUnless(operator.lshift(5, 1) == 10)
  192.         self.failUnless(operator.lshift(5, 0) == 5)
  193.         self.assertRaises(ValueError, operator.lshift, 2, -1)
  194.  
  195.     def test_mod(self):
  196.         self.failUnlessRaises(TypeError, operator.mod)
  197.         self.failUnlessRaises(TypeError, operator.mod, None, 42)
  198.         self.failUnless(operator.mod(5, 2) == 1)
  199.  
  200.     def test_mul(self):
  201.         self.failUnlessRaises(TypeError, operator.mul)
  202.         self.failUnlessRaises(TypeError, operator.mul, None, None)
  203.         self.failUnless(operator.mul(5, 2) == 10)
  204.  
  205.     def test_neg(self):
  206.         self.failUnlessRaises(TypeError, operator.neg)
  207.         self.failUnlessRaises(TypeError, operator.neg, None)
  208.         self.failUnless(operator.neg(5) == -5)
  209.         self.failUnless(operator.neg(-5) == 5)
  210.         self.failUnless(operator.neg(0) == 0)
  211.         self.failUnless(operator.neg(-0) == 0)
  212.  
  213.     def test_bitwise_or(self):
  214.         self.failUnlessRaises(TypeError, operator.or_)
  215.         self.failUnlessRaises(TypeError, operator.or_, None, None)
  216.         self.failUnless(operator.or_(0xa, 0x5) == 0xf)
  217.  
  218.     def test_pos(self):
  219.         self.failUnlessRaises(TypeError, operator.pos)
  220.         self.failUnlessRaises(TypeError, operator.pos, None)
  221.         self.failUnless(operator.pos(5) == 5)
  222.         self.failUnless(operator.pos(-5) == -5)
  223.         self.failUnless(operator.pos(0) == 0)
  224.         self.failUnless(operator.pos(-0) == 0)
  225.  
  226.     def test_pow(self):
  227.         self.failUnlessRaises(TypeError, operator.pow)
  228.         self.failUnlessRaises(TypeError, operator.pow, None, None)
  229.         self.failUnless(operator.pow(3,5) == 3**5)
  230.         self.failUnless(operator.__pow__(3,5) == 3**5)
  231.         self.assertRaises(TypeError, operator.pow, 1)
  232.         self.assertRaises(TypeError, operator.pow, 1, 2, 3)
  233.  
  234.     def test_repeat(self):
  235.         a = range(3)
  236.         self.failUnlessRaises(TypeError, operator.repeat)
  237.         self.failUnlessRaises(TypeError, operator.repeat, a, None)
  238.         self.failUnless(operator.repeat(a, 2) == a+a)
  239.         self.failUnless(operator.repeat(a, 1) == a)
  240.         self.failUnless(operator.repeat(a, 0) == [])
  241.         a = (1, 2, 3)
  242.         self.failUnless(operator.repeat(a, 2) == a+a)
  243.         self.failUnless(operator.repeat(a, 1) == a)
  244.         self.failUnless(operator.repeat(a, 0) == ())
  245.         a = '123'
  246.         self.failUnless(operator.repeat(a, 2) == a+a)
  247.         self.failUnless(operator.repeat(a, 1) == a)
  248.         self.failUnless(operator.repeat(a, 0) == '')
  249.  
  250.     def test_rshift(self):
  251.         self.failUnlessRaises(TypeError, operator.rshift)
  252.         self.failUnlessRaises(TypeError, operator.rshift, None, 42)
  253.         self.failUnless(operator.rshift(5, 1) == 2)
  254.         self.failUnless(operator.rshift(5, 0) == 5)
  255.         self.assertRaises(ValueError, operator.rshift, 2, -1)
  256.  
  257.     def test_contains(self):
  258.         self.failUnlessRaises(TypeError, operator.contains)
  259.         self.failUnlessRaises(TypeError, operator.contains, None, None)
  260.         self.failUnless(operator.contains(range(4), 2))
  261.         self.failIf(operator.contains(range(4), 5))
  262.         self.failUnless(operator.sequenceIncludes(range(4), 2))
  263.         self.failIf(operator.sequenceIncludes(range(4), 5))
  264.  
  265.     def test_setitem(self):
  266.         a = range(3)
  267.         self.failUnlessRaises(TypeError, operator.setitem, a)
  268.         self.failUnlessRaises(TypeError, operator.setitem, a, None, None)
  269.         self.failUnless(operator.setitem(a, 0, 2) is None)
  270.         self.assert_(a == [2, 1, 2])
  271.         self.assertRaises(IndexError, operator.setitem, a, 4, 2)
  272.  
  273.     def test_setslice(self):
  274.         a = range(4)
  275.         self.failUnlessRaises(TypeError, operator.setslice, a)
  276.         self.failUnlessRaises(TypeError, operator.setslice, a, None, None, None)
  277.         self.failUnless(operator.setslice(a, 1, 3, [2, 1]) is None)
  278.         self.assert_(a == [0, 2, 1, 3])
  279.  
  280.     def test_sub(self):
  281.         self.failUnlessRaises(TypeError, operator.sub)
  282.         self.failUnlessRaises(TypeError, operator.sub, None, None)
  283.         self.failUnless(operator.sub(5, 2) == 3)
  284.  
  285.     def test_truth(self):
  286.         class C(object):
  287.             def __nonzero__(self):
  288.                 raise SyntaxError
  289.         self.failUnlessRaises(TypeError, operator.truth)
  290.         self.failUnlessRaises(SyntaxError, operator.truth, C())
  291.         self.failUnless(operator.truth(5))
  292.         self.failUnless(operator.truth([0]))
  293.         self.failIf(operator.truth(0))
  294.         self.failIf(operator.truth([]))
  295.  
  296.     def test_bitwise_xor(self):
  297.         self.failUnlessRaises(TypeError, operator.xor)
  298.         self.failUnlessRaises(TypeError, operator.xor, None, None)
  299.         self.failUnless(operator.xor(0xb, 0xc) == 0x7)
  300.  
  301.     def test_is(self):
  302.         a = b = 'xyzpdq'
  303.         c = a[:3] + b[3:]
  304.         self.failUnlessRaises(TypeError, operator.is_)
  305.         self.failUnless(operator.is_(a, b))
  306.         self.failIf(operator.is_(a,c))
  307.  
  308.     def test_is_not(self):
  309.         a = b = 'xyzpdq'
  310.         c = a[:3] + b[3:]
  311.         self.failUnlessRaises(TypeError, operator.is_not)
  312.         self.failIf(operator.is_not(a, b))
  313.         self.failUnless(operator.is_not(a,c))
  314.  
  315.     def test_attrgetter(self):
  316.         class A:
  317.             pass
  318.         a = A()
  319.         a.name = 'arthur'
  320.         f = operator.attrgetter('name')
  321.         self.assertEqual(f(a), 'arthur')
  322.         f = operator.attrgetter('rank')
  323.         self.assertRaises(AttributeError, f, a)
  324.         f = operator.attrgetter(2)
  325.         self.assertRaises(TypeError, f, a)
  326.         self.assertRaises(TypeError, operator.attrgetter)
  327.         self.assertRaises(TypeError, operator.attrgetter, 1, 2)
  328.  
  329.         class C(object):
  330.             def __getattr(self, name):
  331.                 raise SyntaxError
  332.         self.failUnlessRaises(AttributeError, operator.attrgetter('foo'), C())
  333.  
  334.     def test_itemgetter(self):
  335.         a = 'ABCDE'
  336.         f = operator.itemgetter(2)
  337.         self.assertEqual(f(a), 'C')
  338.         f = operator.itemgetter(10)
  339.         self.assertRaises(IndexError, f, a)
  340.  
  341.         class C(object):
  342.             def __getitem(self, name):
  343.                 raise SyntaxError
  344.         self.failUnlessRaises(TypeError, operator.itemgetter(42), C())
  345.  
  346.         f = operator.itemgetter('name')
  347.         self.assertRaises(TypeError, f, a)
  348.         self.assertRaises(TypeError, operator.itemgetter)
  349.         self.assertRaises(TypeError, operator.itemgetter, 1, 2)
  350.  
  351.         d = dict(key='val')
  352.         f = operator.itemgetter('key')
  353.         self.assertEqual(f(d), 'val')
  354.         f = operator.itemgetter('nonkey')
  355.         self.assertRaises(KeyError, f, d)
  356.  
  357.         # example used in the docs
  358.         inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
  359.         getcount = operator.itemgetter(1)
  360.         self.assertEqual(map(getcount, inventory), [3, 2, 5, 1])
  361.         self.assertEqual(sorted(inventory, key=getcount),
  362.             [('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)])
  363.  
  364. def test_main():
  365.     test_support.run_unittest(OperatorTestCase)
  366.  
  367.  
  368. if __name__ == "__main__":
  369.     test_main()
  370.